#!/bin/bash

# $q: JPEG quality factor
# $i: input PGM file name
# $o: output PGM file name


QUALITY="1 2 3 4 5 6 7 8 9 10"
IMAGES="/home/yhuelf/acTPs/watermarking/wmlab/images/"
UPS="4 2 1"

for i in $(ls $IMAGES | grep pgm); do
	for u in `echo $UPS`; do
		sed -ie s/^U.*/"UPSAMPLING="$u";"/ cfg/embed.cfg
		sed -i -e s/^I.*/"IN=\"images\/$i\";"/ cfg/embed.cfg
		sed -i -e s/^O.*/"OUT=\"images\/marked\/ups_$u\_$i\";"/ cfg/embed.cfg

		./embedder cfg/embed.cfg
	done
done
    

for q in `echo $QUALITY`; do
mkdir $IMAGES"jpeg_compression/quality_"$q
		
	for i in $(ls images/marked/ | grep peppers); do
		cjpeg -quality $q images/marked/$i | djpeg > $IMAGES\
"jpeg_compression/quality_"$q"/"$i
	done

done


for q in  `echo $QUALITY`; do
	echo $q >> upsvar.dat
	for u in `echo $UPS`; do
for i in $(ls images/jpeg_compression/quality_$q | grep ups_$u); do
		sed -ie  s/^I.*/'IN=\"images\/jpeg_compression\/quality_'$q'\/'$i'\";'/ cfg/decode.cfg

		sed -ie s/^U.*/"UPSAMPLING="$u";"/ cfg/decode.cfg
		sed -ie '$s/$/\t/' upsvar.dat
		sed -ie '$s/$/'"$(./decoder cfg/decode.cfg | grep BER | sed s/BER=//)/" upsvar.dat
	done

done
done

gnuplot << EOF
set term png 
set title 'BER against jpeg quality'
set output 'courbes/findBestUpsampl.png'
plot [0:10] [-0.1:0.9] 'upsvar.dat' using 1:2 with points title 'ups 4',\
'upsvar.dat' using 1:3 with points title 'ups 2',\
'upsvar.dat' using 1:4 with points title 'ups 1'
EOF

exit 0
